package com.fiona.common.base.config;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import cn.hutool.core.util.StrUtil;

public class RpcConfig {

	private static String port;
	private static String group;
	private static String transporter;
	private static String address;

	private static Map<String, String> config = new HashMap<String, String>();

	public static void putConfig(Properties properties) {
		properties.forEach((k, v) -> {
			if (k.toString().startsWith("rpc.")) {
				try {
					addVariable(k.toString(), v.toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void addVariable(String key, String value) throws Exception {
		Field[] fields = RpcConfig.class.getDeclaredFields();
		for (Field field : fields) {
			if (key.endsWith(".".concat(field.getName()))) {
				field.setAccessible(true);
				Method m = RpcConfig.class.getMethod("set".concat(StrUtil.upperFirst(field.getName())), String.class);
				m.invoke(null, value);
			}
		}
	}

	public static String getConfig(String key) {
		return config.get(key);
	}

	public static String getPort() {
		return port;
	}

	public static void setPort(String port) {
		RpcConfig.port = port;
	}

	public static String getGroup() {
		return group;
	}

	public static void setGroup(String group) {
		RpcConfig.group = group;
	}

	public static String getTransporter() {
		return transporter;
	}

	public static void setTransporter(String transporter) {
		RpcConfig.transporter = transporter;
	}

	public static String getAddress() {
		return address;
	}

	public static void setAddress(String address) {
		RpcConfig.address = address;
	}

}
